package cn.com.connected.controller.open;

import cn.com.connected.common.emnus.*;
import cn.com.connected.vo.base.OtherDictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(value = "字典接口")
@Slf4j
public class DictController {

    private static OtherDictVO otherDictVO = OtherDictVO.builder()
            .fileType(FileType.toMap())
            .smsType(SmsType.toMap())
            .payStatus(PayStatus.toMap())
            .whether(Whether.toMap())
            .build();


    @PostConstruct
    void init() {

    }

    @ApiOperation(value="描述:字典 ok")
    @RequestMapping(value="/wx/v1/dict/others",method= RequestMethod.GET)
    public OtherDictVO others(HttpServletRequest request, HttpServletResponse response) {

        return otherDictVO;
    }



    @SneakyThrows
    public static String dict(String dictType, Number dictValue){

        if (dictType==null || dictType ==null){
            return null;
        }

        List<Map> types = Collections.emptyList();
        Field[] fields = OtherDictVO.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (dictType.equals(field.getName())){
                types = (List<Map> )field.get(otherDictVO);
            }
        }

        String name = (String) types.stream().filter(o->o.get("value").equals(dictValue)).findFirst().map(z->z.get("name")).orElse(null);
        return name;
    }
    public static String dicts(String dictType, List<Integer> dictValues){
        if (dictType==null || CollectionUtils.isEmpty(dictValues)){
            return null;
        }
        return dictValues.stream().map(o->dict(dictType,o)).collect(Collectors.joining(","));
    }

    /**那种不是特别需要翻译的，就单独在代码里写死就好了，确实需要翻译的要考虑列表循环读库需要提前加载缓存*/
    public static Object dictdb(DictDbType dictDbType, Object dictValue){
        switch (dictDbType){
            default:
                break;
        }
        return "未知翻译类型";
    }
}
